package com.itheima.controller;

import com.itheima.entity.Result;
import com.itheima.pojo.SubmitDto;
import com.itheima.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private HttpServletRequest request;


    @PostMapping("/submit.do")
    public Result submit(@RequestBody SubmitDto submitDto){

        //首先判断验证码
        HttpSession session = request.getSession();
        String code = (String) session.getAttribute(submitDto.getTelephone());
        if (code == null) {
            return new Result(false, "请获取二维码");
        }
        if (!submitDto.getValidateCode().equals(code)) {
            return new Result(false, "二维码不正确");
        }
        //将数据存入表格
        Result result = orderService.submit(submitDto);
        return result;
    }
    @PostMapping("/findById.do")
    public Result findById(Integer id) {
        Result result = orderService.findById(id);
        return result;
    }
}
